package com.tobeface.wechat.message.handler;

import com.google.common.base.Objects;
import com.tobeface.wechat.message.WeChatMessage;
import com.tobeface.wechat.message.WeChatMessageChannel;
import com.tobeface.wechat.message.WeChatMessagePayloads;
import com.tobeface.wechat.message.WeChatMessageTypes;

/**
 * @author loudyn
 */
public abstract class TextMessageHandlerSupport extends WeChatMessageHandlerSupport {

    @Override
    protected final boolean isMessageTypeSupport(String msgType, WeChatMessage message) {
        if (!Objects.equal(msgType, WeChatMessageTypes.TEXT)) {
            return false;
        }

        String content = (String) message.getPayloads().get(WeChatMessagePayloads.CONTENT_PAYLOAD);
        return isTextMessageSupport(message, content);
    }

    protected boolean isTextMessageSupport(WeChatMessage message, String content) {
        return true;
    }

    @Override
    protected final void onMessageReceived(WeChatMessageChannel channel, WeChatMessage message) {
        String content = (String) message.getPayloads().get(WeChatMessagePayloads.CONTENT_PAYLOAD);
        onTextMessageReceived(channel, message, content);
    }

    protected abstract void onTextMessageReceived(WeChatMessageChannel channel, WeChatMessage message, String content);
}
